package Camera;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;

import java.io.IOException;
import java.util.List;

/**
 * Created by freehyan on 9/7/2016.
 */
public class CameraEngine {
    private final static String LOG_TAG = "MyCamera";

    private Camera mCamera;
    private Camera.Parameters mCameraParams;
    private Boolean running = false;

    public void startCamera(SurfaceTexture surfaceTexture){
        Log.v(LOG_TAG, "Starting Camera");

        mCamera = Camera.open(0);
        mCameraParams = mCamera.getParameters();
        List<Camera.Size> sizes = mCamera.getParameters().getSupportedPreviewSizes();
        int LastPos = sizes.size() - 1;

        //mCameraParams.setPreviewSize(sizes.get(LastPos).width, sizes.get(LastPos).height);
        Log.v(LOG_TAG, mCameraParams.getPreviewSize().width + " x " + mCameraParams.getPreviewSize().height);

        // if(getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
//            mCameraParams.set("orientation", "portrait");
//            mCamera.setDisplayOrientation(0);
//        }else{
//            mCameraParams.set("orientation", "landscape");
//            mCamera.setDisplayOrientation(0);
//        }

        try {
            mCamera.setPreviewTexture(surfaceTexture);
            mCamera.startPreview();
            running = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopCamera(){
        if (running) {
            Log.v(LOG_TAG, "Stopping Camera");
            mCamera.stopPreview();
            mCamera.release();
            running = false;
        }
    }
}
